<?php namespace sacvoyage\domain;

use \sacvoyage\storage\Database;

use \sacvoyage\app\AdminApplication;
use \sacvoyage\app\FileApplication;
use \sacvoyage\app\SyncApplication;
use \sacvoyage\app\CustomApplication;

class Property implements Resource {

	public function __construct(User $owner, Database $database) {
		$this->_owner=$owner;
		$this->_database=$database;
	}
	
	public function getOwner() {
		return $this->_owner;
	}
	
	public function getApplication($name) {
		if($name=='admin') {
			return new AdminApplication();	
		} elseif($name=='file') {
			return new FileApplication($this);
		} elseif($name=='sync') {
			return new SyncApplication($this);
		} elseif($name) {
			$config=$this->getCollection('cfg.app')->read($name);
			if($config) {
				return new CustomApplication($this, $config);
			}
		}
	}
	
	public function getCollectionNames() {
		return $this->_database->getCollectionNames();
	}
	
	public function getCollection($name, $permission='full') {
		if(!isset($this->_collectionCache[$name])) {
			$this->_collectionCache[$name]=new Collection(
			    $this->getOwner(),
				$this->_database->getCollection($name),
				$name,
				'full'
			);
		}
		return $this->_collectionCache[$name];
	}
	
	private $_owner;
	private $_database;
	private $_appConfigs;
	
	private function _getAppConfigs() {
		if(!$this->_apps) {
			$this->_apps=$this->getCollection('cfg.apps')->find();
		}
		return $this->_appConfigs;
	}

}

?>
